$(function(){
	list();
})

var defaul = '';
var addressname = '';
var address1 = '';
var address2 = '';
var address3 = '';
var address4 = '';
var defauloption = ''
function list(){
	var htmlList = '';
	var nameList = '';
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_address',
		hs_action_id: 'get_full_list'
	}
	getAjaxRequest(arr,function(result){
		var len = result.length;
		if( len > 0 ) {
			for ( var i = 0; i<len; i++){
				if(result[i].bs_default_ind == 'Y'){
					defaul = "(default)";
					defauloption = 'selected';
				}else if( result[i].bs_default_ind == 'N') {
					defaul = '';
					defauloption = '';
				}
				addressname = result[i].bs_addr_name;
				address1 = result[i].bs_addr_1;
				address2 = result[i].bs_addr_2;
				address3 = result[i].bs_addr_3;
				address4 = result[i].bs_addr_4;

				htmlList += '<div class="item">'+
							'<div class="name" data-key="'+result[i].bj_addr_key+'" data-districtid="'+result[i].bs_district+'" data-addressname="'+addressname+'"><i>'+addressname+'</i><em data-id="'+result[i].bs_default_ind+'"> '+defaul+'</em></div>'+
							'<p>'+address1+','+address2+','+address3+','+address4+'</p>'+
							'<a href="javascript:void(0)" class="edit"><img src="'+MPJ.base+'/resources/images/account/edit.png" /></a>'+
							'<a href="javascript:void(0)" class="close"><img src="'+MPJ.base+'/resources/images/account/delete.png" /></a>'+
							'</div>';
				var temp = addressname+','+address1+','+address2+','+address3+','+address4+','+result[i].bs_district;
				if(defauloption == "selected"){
					nameList += '<option value="'+result[i].bj_addr_key+'" data-temp="'+temp+'" selected>'+addressname+'</option>';
				}else {
					nameList += '<option value="'+result[i].bj_addr_key+'" data-temp="'+temp+'">'+addressname+'</option>';
				}

			}
		}else {
			htmlList = '';
		}
		$('.listBox').html(htmlList);
		$('#pickerBox').html(nameList);
		edit();
		delAddress();
	})
}





//添加新地址
$('.default-address a.mui-btn').on('tap',function(){
	window.location.href = MPJ.base+'/pages/account/receiver/add.jsp';
})


//地址删除
function delAddress() {
	$('.listBox .item').each(function (index) {
		var $this = $(this);
		var $close = $(this).find('a.close');
		var $edit = $(this).find('a.edit');
		var key =  $(this).find('.name').data('key');
		var addressname =  $(this).find('.name').data('addressname');
		$close.on('tap', function () {
			dialog('Message', 'Are you sure want to delete ‘'+addressname+'’ from your address book?', 3);
			var $dialog = $('#dialog');
			$dialog.show();
			$dialog.find('#comfirmBtn').one('click', function () {
				del(key);
				 $dialog.hide();
				 $dialog.remove();
				 $this.remove();
			});
		})
		$edit.on('tap', function () {
			window.location.href = MPJ.base + '/pages/account/receiver/edit.jsp';
		})
	})
}

$('#pickerBox').on('change',function(){
	var val=$('#pickerBox').val();
	var key = $('#pickerBox option:selected').val();
	var temp = $('#pickerBox option:selected').data('temp');
	console.log(temp)
	temp = temp.split(',');
	setDefault(key,temp);
	/*$('.listBox .item').each(function(index){
		var $this=$(this);
		var name=$(this).find('.name i').html();
		if(val==name){
			$('.listBox .name em').html('');
			$('.listBox .item').eq(index).find('em').html('(default)');
		}
	})*/
})

function edit(){
	$('.item').each(function(){
			var $this = $(this).find('.edit');
			var addressName = $(this).find('.name i').html();
			var defaul = $(this).find('.name em').data('id');
			var address = $(this).find('p').html();
			var key = $(this).find('.name').data('key');
			var districtid = $(this).find('.name').data('districtid');
			$this.on('tap',function(){
				sessionStorage.setItem("addressName",addressName);
				sessionStorage.setItem("defaul",defaul);
				sessionStorage.setItem("address",address);
				sessionStorage.setItem("key",key);
				sessionStorage.setItem("districtid",districtid);
				window.location.href = MPJ.base+'/pages/account/receiver/edit.jsp';
		})
	})
}

function del(key){
	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_address',
		hs_action_id: 'delete',
		hs_addr_key: key
	}
	getAjaxRequest(arr,function(result){
		if(result.bs_result == "OK"){
			mui.toast("Del Successfully");
			closeToast();
		}else if(result.bs_result == "ERROR"){
			mui.toast(result.bs_message);
			closeToast();
		}
		list();
	})
}

function setDefault(key,temp){

	var arr = {
		hf_s_id: 'WD11',
		hf_srv_id: 'Cv_jcus_address',
		hs_action_id: 'update',
		hs_addr_1: temp[1],
		hs_addr_2: temp[2],
		hs_addr_3: temp[3],
		hs_addr_4: temp[4],
		hs_district_id: temp[5],
		hs_addr_name: temp[0],
		hs_addr_key:key,
		hs_def_addr_ind:'Y',
	}
	getAjaxRequest(arr,function(result){
		list();
	})
}
function closeToast(){
	setTimeout(function(){
		$('.mui-toast-container').remove();
	},1500)
}

$('.save').on('tap',function(){
	save();
})